package com.jiang.myfm.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jiang.myfm.MainActivity;
import com.jiang.myfm.R;
import com.jiang.myfm.adapters.MainHeadImageAdapter;
import com.jiang.myfm.adapters.MainHeadTwoAdapter;
import com.jiang.myfm.adapters.MainRecyclerAdapterMore;
import com.jiang.myfm.adapters.MainRecyclerAdapterOne;
import com.jiang.myfm.adapters.MainRecyclerAdapterSpecial;
import com.jiang.myfm.adapters.MainRecyclerAdapterTwo;
import com.jiang.myfm.entities.mainpagetwo.DataListGuess;
import com.jiang.myfm.entities.mainpagetwo.EntityMainTwo;
import com.jiang.myfm.entities.mainpagetwo.ListHotRecommends;
import com.jiang.myfm.entities.mianpage.EntityMainOne;
import com.jiang.myfm.entities.mianpage.ListEditorRecommendAlbums;
import com.jiang.myfm.entities.mianpage.ListSpecialColumn;
import com.jiang.myfm.interfaces.MainInternetServiceOne;
import com.jiang.myfm.interfaces.MainInternetServiceTwo;
import com.jiang.myfm.newview.CustomRelativeLayout;
import com.jiang.myfm.newview.CustomViewPager;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class Findfragment_1 extends Fragment {

    private SwipeRefreshLayout mSwipeRefreshLayout;

    private RecyclerView mRecyclerViewOne;
    private RecyclerView mRecyclerViewRecommending;
    private RecyclerView mRecyclerViewSpecial;
    private RecyclerView mRecyclerViewMore;

    private List<ListEditorRecommendAlbums> mMainRecyclerRecommendingDataList;
    private List<ListSpecialColumn> mMainRecyclerSpecialDataList;
    private List<DataListGuess> mMainRecyclerOneDataList;
    private List<Fragment> mFragmentList, mFragmentListTwo;
    private List<ListHotRecommends> mListHotRecommends;

    private TextView mTitleOneTextViewOne, mTextViewRecommending, mTextViewSpecial;

    private CustomViewPager mViewPagerHead, mViewPagerHeadTwo;
    private CustomRelativeLayout mCustomRelativeLayout;
    private CustomRelativeLayout mCustomRelativeLayoutRecommending;
    private CustomRelativeLayout mCustomRelativeLayoutSpecial;
    private CustomRelativeLayout mCustomRelativeLayoutMoreCategory;

    private EntityMainOne mEntityMainOne;
    private EntityMainTwo mEntityMainTwo;

    private Fragment mFragmentHeadImage, mFragmentHeadTwo;

    //不要再用一个adapter搞两个viewPager了,崩了不报代码错误
    private MainHeadImageAdapter mPagerAdapter;
    private MainHeadTwoAdapter mPagerTwoAdapter;
    private MainRecyclerAdapterOne mMainRecyclerAdapterOne;
    private MainRecyclerAdapterTwo mMainRecyclerAdapterTwo;
    private MainRecyclerAdapterMore mMainRecyclerAdapterMore;
    private MainRecyclerAdapterSpecial mMainRecyclerAdapterSpecial;

    public Findfragment_1() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View ret = inflater.inflate(R.layout.fragment_findfragment_1, container, false);
        //初始化控件
        initView(ret);
        //初始化adapter
        initAdapter();
        //初始化数据
        initData();
        return ret;
    }

    private void initAdapter() {
        if (mPagerAdapter != null) {
            mPagerAdapter = null;
        }
        mPagerAdapter = new MainHeadImageAdapter(getFragmentManager());
        mPagerTwoAdapter = new MainHeadTwoAdapter(getFragmentManager());
        mMainRecyclerAdapterOne = new MainRecyclerAdapterOne(mMainRecyclerOneDataList, getContext());
        mMainRecyclerAdapterTwo = new MainRecyclerAdapterTwo(mMainRecyclerRecommendingDataList, getContext());
        mMainRecyclerAdapterSpecial = new MainRecyclerAdapterSpecial(mMainRecyclerSpecialDataList, getContext());
        mMainRecyclerAdapterMore = new MainRecyclerAdapterMore(mListHotRecommends, getContext());

        mViewPagerHead.setAdapter(mPagerAdapter);
        mViewPagerHeadTwo.setAdapter(mPagerTwoAdapter);
        mRecyclerViewOne.setAdapter(mMainRecyclerAdapterOne);
        mRecyclerViewRecommending.setAdapter(mMainRecyclerAdapterTwo);
        mRecyclerViewSpecial.setAdapter(mMainRecyclerAdapterSpecial);
        mRecyclerViewMore.setAdapter(mMainRecyclerAdapterMore);

        //recyclerView管理每行个数,不同的recyclerView需要分开new,不能用一个,虽然都是一样的
        GridLayoutManager manager = new GridLayoutManager(getContext(), 3);
        mRecyclerViewOne.setLayoutManager(manager);
        GridLayoutManager managerRecommending = new GridLayoutManager(getContext(), 3);
        mRecyclerViewRecommending.setLayoutManager(managerRecommending);
        GridLayoutManager managerSpecial = new GridLayoutManager(getContext(), 1);
        mRecyclerViewSpecial.setLayoutManager(managerSpecial);
        GridLayoutManager managerMore = new GridLayoutManager(getContext(), 1);
        mRecyclerViewMore.setLayoutManager(managerMore);

    }

    private void initData() {
        MainInternetServiceOne mainInternetServiceOne = new Retrofit.Builder()
                .baseUrl("http://mobile.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(MainInternetServiceOne.class);
        mainInternetServiceOne.getText().enqueue(new Callback<EntityMainOne>() {
            @Override
            public void onResponse(Call<EntityMainOne> call, Response<EntityMainOne> response) {
                mEntityMainOne = response.body();
//--------------------------------------------
                for (int i = 0; i < mEntityMainOne.getFocusImages().getList().size(); i++) {
                    String picUrl = mEntityMainOne.getFocusImages().getList().get(i).getPic();
                    mFragmentHeadImage = new MainHeadImageFragment();
                    Bundle bundle = new Bundle();
                    bundle.putString("picUrl", picUrl);
                    mFragmentHeadImage.setArguments(bundle);
                    mFragmentList.add(mFragmentHeadImage);
                }
                mPagerAdapter.addAll(mFragmentList);
//--------------------------------------------
                for (int i = 0; i < mEntityMainOne.getDiscoveryColumns().getList().size() / 4; i++) {
                    mFragmentHeadTwo = new MainHeadTwoFragment();
                    Bundle bundle = new Bundle();
                    for (int j = 0; j < 4; j++) {
                        int index = 4 * i + j;
                        String title = mEntityMainOne.getDiscoveryColumns().getList().get(index).getTitle();
                        String coverPath = mEntityMainOne.getDiscoveryColumns().getList().get(index).getCoverPath();
                        bundle.putString("title" + j, title);
                        bundle.putString("coverPath" + j, coverPath);
                    }
                    mFragmentHeadTwo.setArguments(bundle);
                    mFragmentListTwo.add(mFragmentHeadTwo);
                }
                mPagerTwoAdapter.addAll(mFragmentListTwo);
//--------------------------------------------
                mTextViewRecommending.setText(mEntityMainOne.getEditorRecommendAlbums().getTitle());
                mMainRecyclerAdapterTwo.addAll(mEntityMainOne.getEditorRecommendAlbums().getList());
//--------------------------------------------
                mTextViewSpecial.setText(mEntityMainOne.getSpecialColumn().getTitle());
                mMainRecyclerAdapterSpecial.addAll(mEntityMainOne.getSpecialColumn().getList());
            }

            @Override
            public void onFailure(Call<EntityMainOne> call, Throwable t) {

            }
        });
        MainInternetServiceTwo mainInternetServiceTwo = new Retrofit.Builder()
                .baseUrl("http://mobile.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(MainInternetServiceTwo.class);
        mainInternetServiceTwo.getText().enqueue(new Callback<EntityMainTwo>() {
            @Override
            public void onResponse(Call<EntityMainTwo> call, Response<EntityMainTwo> response) {
                mEntityMainTwo = response.body();
//--------------------------------------------
                mTitleOneTextViewOne.setText(mEntityMainTwo.getGuess().getTitle());
                mMainRecyclerAdapterOne.addAll(mEntityMainTwo.getGuess().getList());
//--------------------------------------------
                mMainRecyclerAdapterMore.addAll(mEntityMainTwo.getHotRecommends().getList());
            }

            @Override
            public void onFailure(Call<EntityMainTwo> call, Throwable t) {

            }
        });

    }

    private void initView(View ret) {
        mFragmentList = new ArrayList<>();
        mFragmentListTwo = new ArrayList<>();
        mMainRecyclerOneDataList = new ArrayList<>();
        mMainRecyclerRecommendingDataList = new ArrayList<>();
        mMainRecyclerSpecialDataList = new ArrayList<>();
        mListHotRecommends = new ArrayList<>();

        mRecyclerViewOne = (RecyclerView) ret.findViewById(R.id.find_1_recycler_view_one);
        mRecyclerViewRecommending = (RecyclerView) ret.findViewById(R.id.find_1_recycler_view_recommending);
        mRecyclerViewSpecial = (RecyclerView) ret.findViewById(R.id.find_1_recycler_view_special);
        mRecyclerViewMore = (RecyclerView) ret.findViewById(R.id.find_1_recycler_view_more);

        mCustomRelativeLayoutRecommending = (CustomRelativeLayout) ret.findViewById(R.id.find_1_title_recommending);
        mCustomRelativeLayoutRecommending.setMaxHeight(MainActivity.MAX_HEIGHT_TITLE);
        mCustomRelativeLayoutSpecial = (CustomRelativeLayout) ret.findViewById(R.id.find_1_title_special);
        mCustomRelativeLayoutSpecial.setMaxHeight(MainActivity.MAX_HEIGHT_TITLE);
        mCustomRelativeLayoutMoreCategory = (CustomRelativeLayout) ret.findViewById(R.id.find_1_more_category);
        mCustomRelativeLayoutMoreCategory.setMaxHeight(MainActivity.MAX_HEIGHT_TITLE);

        mTextViewRecommending = (TextView) ret.findViewById(R.id.find_1_title_recommending_tv1);
        mTextViewSpecial = (TextView) ret.findViewById(R.id.find_1_title_special_tv1);


        mViewPagerHeadTwo = (CustomViewPager) ret.findViewById(R.id.find_1_viewpager_2);
        mViewPagerHeadTwo.setMaxHeight((int) (getResources().getDisplayMetrics().widthPixels / 3.5));

        mViewPagerHead = (CustomViewPager) ret.findViewById(R.id.find_1_viewpager);
        //自定义viewpager,添加设置高度的方法
        mViewPagerHead.setMaxHeight((int) (getResources().getDisplayMetrics().widthPixels / 2.13));
//        mViewPagerHead.setMaxHeight(330);

        mTitleOneTextViewOne = (TextView) ret.findViewById(R.id.find_1_title1_tv1);

        mCustomRelativeLayout = (CustomRelativeLayout) ret.findViewById(R.id.find_1_title1);
        mCustomRelativeLayout.setMaxHeight(MainActivity.MAX_HEIGHT_TITLE);

        mSwipeRefreshLayout = (SwipeRefreshLayout) ret.findViewById(R.id.find_1_scroll);

        //mSwipeRefreshLayout触摸事件与mViewPagerHead冲突的处理
        mViewPagerHead.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                touchEvent(event);
                return false;
            }
        });
        mViewPagerHeadTwo.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                touchEvent(event);
                return false;
            }
        });
    }

    private void touchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                mSwipeRefreshLayout.setEnabled(false);
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                mSwipeRefreshLayout.setEnabled(true);
                break;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mPagerAdapter.reSetData();
        mPagerTwoAdapter.reSetData();
    }
}
